Corrigé - Atelier A1 - Console avancée
Essayez de faire l'atelier avant de consulter ce corrigé.
Sans blessed (codes ANSI)
Numéro 1
nom: str = "Philippe"
age: int = 30
print(f"\033[1;32m{nom}\033[0m, {age} ans")
Numéro 2
print("\033[2J\033[3;10HBienvenue!", end='')
Numéro 3
import time
print("\033[?25l", end='')
for i in range(5, 0, -1):
print("\033[H", end='')
print("┌───┐")
print(f"│ {i} │")
print("└───┘")
time.sleep(1)
print("\033[H", end='')
print("BOOM")
print("\033[?25h", end='')
Numéro 4
import time
largeur: int = 10
hauteur: int = 5
ligne: int
colonne: int
for ligne in range(hauteur, 0, -1):
for colonne in range(largeur, 0, -1):
print(f"\033[{ligne};{colonne}H*", end='', flush=True)
time.sleep(0.05)
print()
Avec blessed
Numéro 5
from blessed import Terminal
term = Terminal()
print(f"Largeur: {term.width}, hauteur: {term.height}")
Numéro 6
Numéro 1:
from blessed import Terminal
term = Terminal()
nom: str = "Philippe"
age: int = 30
print(term.bold_green(nom) + f", {age} ans")
Numéro 2:
from blessed import Terminal
term = Terminal()
print(term.clear + term.move_xy(9, 2) + "Bienvenue!", end='')
Numéro 3:
import time
from blessed import Terminal
term = Terminal()
print(term.hide_cursor, end='')
for i in range(5, 0, -1):
print(term.home, end='')
print("┌───┐")
print(f"│ {i} │")
print("└───┘")
time.sleep(1)
print(term.home, end='')
print("BOOM")
print(term.normal_cursor, end='')
Numéro 7
from blessed import Terminal
term = Terminal()
touche: str
print("Appuyez sur une touche...")
with term.cbreak():
touche = term.inkey()
print(f"Vous avez appuyé sur: {touche}")
Avec blessed - Petits projets
Numéro 8
import time
from blessed import Terminal
term = Terminal()
largeur: int = 20
pourcentage: int
rempli: int
barre: str
ligne: str
for pourcentage in range(0, 101, 5):
rempli = round(pourcentage / 100 * largeur)
barre = "#" * rempli + "-" * (largeur - rempli)
ligne = f"[{barre}] {pourcentage}%"
if pourcentage < 50:
print(f"\r{term.red(ligne)}", end='')
elif pourcentage < 80:
print(f"\r{term.yellow(ligne)}", end='')
else:
print(f"\r{term.green(ligne)}", end='')
time.sleep(0.1)
print()
Numéro 9
from blessed import Terminal
term = Terminal()
texte1: str = "Nouvelle partie"
texte2: str = "Options"
texte3: str = "Quitter"
selection: int = 0
touche = None
print(term.hide_cursor, end='')
while touche is None or touche.name != 'KEY_ENTER':
print(term.home + term.clear_eos, end='')
print(term.reverse(f"> {texte1}") if selection == 0 else f" {texte1}")
print(term.reverse(f"> {texte2}") if selection == 1 else f" {texte2}")
print(term.reverse(f"> {texte3}") if selection == 2 else f" {texte3}")
with term.cbreak():
touche = term.inkey()
if touche.name == 'KEY_UP' and selection > 0:
selection -= 1
elif touche.name == 'KEY_DOWN' and selection < 2:
selection += 1
print(term.normal_cursor, end='')
match selection:
case 0:
print(f"Vous avez choisi: {texte1}")
case 1:
print(f"Vous avez choisi: {texte2}")
case 2:
print(f"Vous avez choisi: {texte3}")
Numéro 10
from blessed import Terminal
term = Terminal()
taille: int = 5
ligneJoueur: int = 0
colonneJoueur: int = 0
murHaut: str
murBas: str
ligne: int
colonne: int
touche = None
murHaut = "┌" + "─" * taille + "┐"
murBas = "└" + "─" * taille + "┘"
print(term.hide_cursor, end='')
print(murHaut)
for ligne in range(taille):
print("│", end='')
for colonne in range(taille):
if ligne == 0 and colonne == 0:
print("@", end='')
elif ligne == taille - 1 and colonne == taille - 1:
print("X", end='')
else:
print(" ", end='')
print("│")
print(murBas)
while ligneJoueur != taille - 1 or colonneJoueur != taille - 1:
with term.cbreak():
touche = term.inkey()
print(term.move_xy(colonneJoueur + 1, ligneJoueur + 1) + " ", end='')
if touche.name == 'KEY_UP' and ligneJoueur > 0:
ligneJoueur -= 1
elif touche.name == 'KEY_DOWN' and ligneJoueur < taille - 1:
ligneJoueur += 1
elif touche.name == 'KEY_LEFT' and colonneJoueur > 0:
colonneJoueur -= 1
elif touche.name == 'KEY_RIGHT' and colonneJoueur < taille - 1:
colonneJoueur += 1
print(term.move_xy(colonneJoueur + 1, ligneJoueur + 1) + "@", end='')
print(term.move_xy(0, taille + 2) + term.normal_cursor, end='')
print("Vous avez trouvé la sortie!")
Modes alternatifs: pour une cible aléatoire, remplacez taille - 1, taille - 1 par 2 variables ligneCible/colonneCible générées avec random.randint(0, taille - 1). Pour l'agrandissement progressif, mettez toute la boucle while dans une autre boucle while taille <= 10, en incrémentant taille de 1 à chaque sortie trouvée.
Numéro 11
import time
import random
from blessed import Terminal
term = Terminal()
selection: int
touche: str
taille: int
portee: int
ligneJoueur: int
colonneJoueur: int
ligneTresor: int
colonneTresor: int
debut: float
distance: int
pourcentage: int
rempli: int
barre: str
tempsEcoule: float
selection = 0
touche = None
print(term.hide_cursor, end='')
while touche is None or touche.name != 'KEY_ENTER':
print(term.home + term.clear_eos, end='')
print(term.reverse("> Facile (pièce 10x10, détecteur à 8 cases)") if selection == 0 else " Facile (pièce 10x10, détecteur à 8 cases)")
print(term.reverse("> Moyen (pièce 20x20, détecteur à 5 cases)") if selection == 1 else " Moyen (pièce 20x20, détecteur à 5 cases)")
print(term.reverse("> Difficile (pièce 30x30, détecteur à 3 cases)") if selection == 2 else " Difficile (pièce 30x30, détecteur à 3 cases)")
with term.cbreak():
touche = term.inkey()
if touche.name == 'KEY_UP' and selection > 0:
selection -= 1
elif touche.name == 'KEY_DOWN' and selection < 2:
selection += 1
match selection:
case 0:
taille = 10
portee = 8
case 1:
taille = 20
portee = 5
case 2:
taille = 30
portee = 3
ligneJoueur = taille // 2
colonneJoueur = taille // 2
ligneTresor = random.randint(0, taille - 1)
colonneTresor = random.randint(0, taille - 1)
debut = time.time()
while ligneJoueur != ligneTresor or colonneJoueur != colonneTresor:
distance = abs(ligneJoueur - ligneTresor) + abs(colonneJoueur - colonneTresor)
pourcentage = round(max(0, 100 - distance / portee * 100))
rempli = round(pourcentage / 100 * 20)
barre = "#" * rempli + "-" * (20 - rempli)
print(term.home + term.clear_eos, end='')
print(f"[{barre}] {pourcentage}%")
with term.cbreak():
touche = term.inkey()
if touche.name == 'KEY_UP' and ligneJoueur > 0:
ligneJoueur -= 1
elif touche.name == 'KEY_DOWN' and ligneJoueur < taille - 1:
ligneJoueur += 1
elif touche.name == 'KEY_LEFT' and colonneJoueur > 0:
colonneJoueur -= 1
elif touche.name == 'KEY_RIGHT' and colonneJoueur < taille - 1:
colonneJoueur += 1
tempsEcoule = time.time() - debut
print(term.normal_cursor, end='')
print(f"Trésor trouvé en {tempsEcoule:.1f} secondes!")